//
//  JXExpressTest
//  Created by Aaron on 2017/4/6.
//  Copyright © 2017年 Zhu jinxin. All rights reserved.

#import "JXExpressInfo.h"
#import <UIKit/UIKit.h>
#import <CommonCrypto/CommonDigest.h>
#import "AFNetworking.h"


@implementation JXExpressInfo

+ (void)getExpressInfo:(NSDictionary *)parameters block:(void (^)(NSInteger, NSDictionary *, NSString *))block  {
    
    // 快递鸟  注册时的字段   填写你的app对应的字段
    NSString *EBusinessID = @"xxxxxx";
    NSString *AppKey = @"xxxxxxxxxxxxxxxxxxxxx";
    // 快递鸟  快递信息查询时不同的类型对应不同的链接
    NSString *ReqURL = @"http://api.kdniao.cc/Ebusiness/EbusinessOrderHandle.aspx";
    
    //para1 - requestData
    NSString *requestStr = [NSString stringWithFormat:@"{\"LogisticCode\":\"%@\",\"ShipperCode\":\"%@\"}",parameters[@"expNo"],parameters[@"expCode"]];
    
    NSLog(@"requestStr:%@",requestStr);
    
    NSString *encodePath = [JXExpressInfo urlEncoder:requestStr];
    NSLog(@"encodePath:%@",encodePath);
    
    NSString *dataSign = [JXExpressInfo encrypt:requestStr keyValue:AppKey charset:@"UTF-8"];
    NSLog(@"datasign:%@",dataSign);
    
    NSString *dataSignEncoder = (NSString *)CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,
                                                                                                      (CFStringRef)dataSign,
                                                                                                      NULL,
                                                                                                      CFSTR(":/?#[]@!$ &'()*+,;=\"<>%{}|\\^~`"),
                                                                                                      kCFStringEncodingUTF8));
    
    
    
    NSLog(@"dataSignEncoder:%@",dataSignEncoder);
    
    
    //请求参数
    NSDictionary *requestParameters = @{@"RequestData":requestStr,
                                        @"EBusinessID":EBusinessID,
                                        @"RequestType":@"1002",
                                        @"DataSign":dataSignEncoder,
                                        @"DataType":@"2"};
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", @"text/html", @"text/xml", @"text/plain", nil];
    
    
    [manager POST:ReqURL parameters:requestParameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        block(1,responseObject,nil);
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        ; NSLog(@"errorCode:%ld",(long)error.code);
    }];
    
}


+ (NSString *)urlEncoder:(NSString *)urlStr{
    
    NSString *encodePath = nil;
    if ([[UIDevice currentDevice].systemVersion floatValue] > 7.0) {
        encodePath = [urlStr stringByAddingPercentEncodingWithAllowedCharacters:NSCharacterSet.URLQueryAllowedCharacterSet];
    }else{
        encodePath = [urlStr stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet characterSetWithCharactersInString:@"`#%^{}\"[]|\\<> "].invertedSet];
    }
    
    return encodePath;
}

+ (NSString *) encrypt:(NSString *)content keyValue:(NSString *)keyValue charset:(NSString *)charset{
    
    return [JXExpressInfo base64:[JXExpressInfo MD5:[NSString stringWithFormat:@"%@%@",content,keyValue] charset:charset] charset:charset];
    
    
}


+ (NSString *)base64:(NSString *)str charset:(NSString *)charset{
    
    return [JXExpressInfo base64Encode:str];
}

//base64编码
+ (NSString *)base64Encode:(NSString *)dataStr{
    NSData *data = [dataStr dataUsingEncoding:kCFStringEncodingUTF8];
    NSLog(@"Base64Str:%@",[data base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength]);
    return [data base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
}

//MD5
+ (NSString *)MD5:(NSString *)str charset:(NSString *)charset{
    
    const char *cStr = [str UTF8String];
    unsigned char digest[CC_MD5_DIGEST_LENGTH];
    
    CC_MD5(cStr, (CC_LONG)strlen(cStr), digest);
    
    NSMutableString *result = [NSMutableString string];
    for (int i = 0; i < CC_MD5_DIGEST_LENGTH; i++) {
        [result appendFormat:@"%02X", digest[i]];
    }
    return [result lowercaseString] ;
}

@end
